package com.sunxd.vue.infrastructure.adapter.db.mapping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sunxd.vue.domain.bean.domain.User;
import com.sunxd.vue.domain.bean.domain.interactive.ActivityConfig;
import com.sunxd.vue.domain.bean.domain.interactive.InteractiveActivity;
import com.sunxd.vue.infrastructure.adapter.db.po.InteractiveActivityPO;
import com.sunxd.vue.infrastructure.adapter.db.po.UserPO;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;
import org.mapstruct.ReportingPolicy;

import static org.mapstruct.factory.Mappers.getMapper;

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface InteractiveActivityMapping extends BaseMapping {

    InteractiveActivityMapping CONVERTER = getMapper(InteractiveActivityMapping.class);

    @Mappings({
            @Mapping(target = "actConfig", source = "actConfig" ,qualifiedByName = "obj2String"),
            @Mapping(target = "uuid", source = "uuid",qualifiedByName = "initUuid")
    })
    InteractiveActivityPO do2create(InteractiveActivity param);

    @Mappings({
            @Mapping(target = "actConfig", source = "actConfig" ,qualifiedByName = "obj2String"),
    })
    InteractiveActivityPO do2po(InteractiveActivity param);

    @Mappings({
            @Mapping(target = "actConfig", source = "actConfig" ,qualifiedByName = "str2ActivityConfig"),
    })
    InteractiveActivity po2do(InteractiveActivityPO param);

    @Named(value = "str2ActivityConfig")
    default ActivityConfig str2ActivityConfig(String str){
        if(StringUtils.isBlank(str)){
            return null;
        }
        return JSON.parseObject(str,new TypeReference<ActivityConfig>(){});
    }

}
